package com.jike.jkgame;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jike.jkgame.data.Status;
import com.jike.jkgame.data.UserStatus;

public class UserStatusFragment extends Fragment implements OnClickListener {

	public static final int FLAG_ALL = 0xFFFFFFFF;
	public static final int EXPERIENCE = 1;
	public static final int ENERGY = 1 << 1;;
	public static final int VIGOUR = 1 << 2;
	public static final int MONEY = 1 << 3;
	public static final int CASH = 1 << 4;
	public static final int LEVEL = 1 << 5;

	public static final int STATIC = 1 << 31;

	private boolean mClickable = true;
	private ViewHolder mViewHolder;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.userstatus_layout, container);
		v.setOnClickListener(this);

		bindView(v);

		return v;
	}

	public void setClickable(boolean clickable) {
		mClickable = clickable;
	}

	@Override
	public void onClick(View v) {
		if (mClickable) {
			Intent intent = new Intent(UserStatusFragment.this.getActivity(),
					UserInfoActivity.class);

			startActivity(intent);
		}
	}

	private void bindView(View parent) {
		if (mViewHolder == null) {
			mViewHolder = new ViewHolder();
		}

		Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
				"font.ttf");

		mViewHolder.nameTv = (TextView) parent.findViewById(R.id.tv_username);
		mViewHolder.nameTv.setTypeface(font);

		mViewHolder.levelTv = (TextView) parent.findViewById(R.id.tv_level);
		mViewHolder.levelTv.setTypeface(font);
		mViewHolder.experiencePb = (ProgressBar) parent
				.findViewById(R.id.pb_level);

		mViewHolder.energyPb = (ProgressBar) parent
				.findViewById(R.id.pb_energy);
		mViewHolder.energyTimerTv = (TextView) parent
				.findViewById(R.id.tv_energy_timer);

		mViewHolder.vigourPb = (ProgressBar) parent
				.findViewById(R.id.pb_vigour);
		mViewHolder.vigourTimerTv = (TextView) parent
				.findViewById(R.id.tv_vigour_timer);

		mViewHolder.coinsTv = (TextView) parent.findViewById(R.id.tv_coins);
		mViewHolder.goldsTv = (TextView) parent.findViewById(R.id.tv_golds);

		updateView(FLAG_ALL);
	}

	public void updateEnergyTimer(int seconds) {
		mViewHolder.energyTimerTv.setText(Utilis.fromatTime(seconds));
	}

	public void updateVigourTimer(int seconds) {
		mViewHolder.vigourTimerTv.setText(Utilis.fromatTime(seconds));
	}

	static class ViewHolder {
		public TextView nameTv;
		public TextView levelTv;
		public ProgressBar experiencePb;
		// public TextView experienceTv;
		public ProgressBar energyPb;
		// public TextView energyTv;
		public TextView energyTimerTv;
		public ProgressBar vigourPb;
		// public TextView strengthTv;
		public TextView vigourTimerTv;

		public TextView coinsTv;
		public TextView goldsTv;
	}

	public void updateView(int flag) {
		if (flag == 0) {
			return;
		}
		UserStatus user = UserStatus.get();

		if ((flag & STATIC) != 0) {
			mViewHolder.nameTv.setText(user.getName());
		}

		if ((flag & LEVEL) != 0) {
			mViewHolder.levelTv.setText(String.valueOf(user.getLevel()));
		}

		if ((flag & EXPERIENCE) != 0) {
			// 经验
			mViewHolder.experiencePb.setMax(user.getFullExperience());
			mViewHolder.experiencePb.setProgress(user.getExperience());
			// mViewHolder.experienceTv.setText(String.format("%d/%d",
			// user.getExperience(), user.getFullExperience()));
		}

		if ((flag & ENERGY) != 0) {
			// 能量
			Status status = user.getEnergy();
			mViewHolder.energyPb.setMax(status.maxValue);
			mViewHolder.energyPb.setProgress(status.value);
			// mViewHolder.energyTv.setText(String.format("%d/%d", status.value,
			// status.maxValue));
		}

		if ((flag & VIGOUR) != 0) {
			// 精力
			Status status = user.getVigour();
			mViewHolder.vigourPb.setMax(status.maxValue);
			mViewHolder.vigourPb.setProgress(status.value);
			// mViewHolder.strengthTv.setText(String.format("%d/%d",
			// status.value,
			// status.maxValue));

		}

		if ((flag & MONEY) != 0) {
			mViewHolder.coinsTv.setText(String.valueOf(user.getMoney()));
		}

		if ((flag & CASH) != 0) {
			mViewHolder.goldsTv.setText(String.valueOf(user.getCash()));
		}
	}

}
